home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Securite / Password Safe / pwsafe-3.35.exe / KPV1_to_PWS.xslt < prev    next >
Extensible Markup Language  |  2014-07-26  |  8KB  |  176 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3.   Copyright (c) 2003-2014 Rony Shapiro <ronys@users.sourceforge.net>.
  4.   All rights reserved. Use of the code is allowed under the
  5.   Artistic License 2.0 terms, as specified in the LICENSE file
  6.   distributed with this code, or available from
  7.   http://www.opensource.org/licenses/artistic-license-2.0.php
  8.  
  9.   This file will convert XML files exported by KeePass V1 (tested with KeePass v1.19b)
  10.   into the equivalent XML files that can be imported into PasswordSafe V3.26 or later.
  11.  
  12.   The delimiter attribute in the 'passwordsafe' element is set to "right angle quotation
  13.   marks" and any carriage returns/line feeds in the Notes field will be converted this
  14.   character to enable the Notes field to be processed correctly by PasswordSafe during import.
  15.  
  16.   KeePass exports the Group Tree structure using nested groups. PasswordSafe uses a dot ('.').
  17.   Therefore this XSLT code first changes any dot in the group field to a forward slash ('/')
  18.   and then any backward slashes to dots.  This will create the correct group path in
  19.   Password Safe but any dots in the original group field will remain a forward slash.
  20.  
  21.   This XSLT file conforms to V1.0 of XSLT as described in http://www.w3.org/TR/xslt
  22.  
  23.   Under Windows, the following 3 programs can process this file:
  24.   a. Command line program msxml.exe from Microsoft (Note: the executable states it
  25.      is V1.1.0.1 but the website says it is V2.0).
  26.   b. AltovaXML Community Edition (current version 2011r3). 
  27.      See http://www.altova.com/altovaxml.html.
  28.   c. Saxon-HE (Home Edition) (current version 9.3.0.5).
  29.      See http://saxon.sourceforge.net/.
  30.      There is also a graphical front-end for Saxon called Kernow.
  31.      See http://kernowforsaxon.sourceforge.net
  32.  
  33.   All programs are free.
  34. -->
  35.  
  36. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  37.          xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
  38.   <xsl:output method="xml" encoding="UTF-8" indent="yes"
  39.          cdata-section-elements="group title password username url notes uuid"/>
  40.  
  41.   <xsl:variable name="delimiter" select="'»'" />
  42.   <xsl:variable name="crlf" select="' '"/>
  43.  
  44.   <xsl:template match="/">
  45.     <passwordsafe>
  46.       <xsl:attribute name="xsi:noNamespaceSchemaLocation"
  47.             namespace="http://www.w3.org/2001/XMLSchema-instance">pwsafe.xsd</xsl:attribute>
  48.       <xsl:attribute name="delimiter"><xsl:value-of select="$delimiter" /></xsl:attribute>
  49.  
  50.       <xsl:for-each select="pwlist/pwentry">
  51.         <xsl:variable name="vgroup" select="translate(group, '.', '/')"/>
  52.         <xsl:variable name="vtemp1" select="string(group/@tree)"/>
  53.         <xsl:variable name="vtemp2" select="translate($vtemp1, '.', '/')"/>
  54.         <xsl:variable name="vgrouptree" select="translate($vtemp2, '\', '.')"/>
  55.         <xsl:variable name="vnotes" select="notes"/>
  56.         <xsl:variable name="vexpiretime" select="expiretime"/>
  57.         <xsl:variable name="vexpires" select="string($vexpiretime/@expires)"/>
  58.         <xsl:variable name="vlastmodtime" select="string(lastmodtime)"/>
  59.         <xsl:value-of select="$crlf" disable-output-escaping="yes"/>
  60.         <xsl:value-of select="$crlf" disable-output-escaping="yes"/>
  61.  
  62.         <entry>
  63.           <xsl:attribute name="id">
  64.             <xsl:value-of select="position()"/>
  65.           </xsl:attribute>
  66.           <xsl:choose>
  67.             <xsl:when test="string(normalize-space($vgrouptree)) != ''">
  68.             <xsl:variable name="vgroups" select="concat($vgrouptree, '.', $vgroup)"/>
  69.             <group>
  70.               <xsl:value-of select="string($vgroups)"/>
  71.             </group>
  72.             </xsl:when>
  73.             <xsl:otherwise>
  74.             <group>
  75.               <xsl:value-of select="string($vgroup)"/>
  76.             </group>
  77.             </xsl:otherwise>
  78.           </xsl:choose>
  79.           <title>
  80.             <xsl:value-of select="string(title)"/>
  81.           </title>
  82.           <username>
  83.             <xsl:value-of select="string(username)"/>
  84.           </username>
  85.           <password>
  86.             <xsl:value-of select="string(password)"/>
  87.           </password>
  88.           <url>
  89.             <xsl:value-of select="string(url)"/>
  90.           </url>
  91.           <notes>
  92.             <!-- Here we have to remove character \r and string 
  93.                  and replace character \n and string   by the delimiter
  94.                  Which ones actually do the work depends on how the user exported
  95.                  the XML. Checking "Encode/Replace newline characters with '\n'"
  96.                  gives an XML file with the strings '\r\n', unchecking this option
  97.                  gives an XML file with the strings ' '.
  98.             -->
  99.             <xsl:variable name="vnotes_1">
  100.               <xsl:call-template name="string-replace-all">
  101.                 <xsl:with-param name="text" select="$vnotes"/>
  102.                 <xsl:with-param name="replace" select="'\n'"/>
  103.                 <xsl:with-param name="by" select="$delimiter"/>
  104.               </xsl:call-template>
  105.             </xsl:variable>
  106.             <xsl:variable name="vnotes_2">
  107.               <xsl:call-template name="string-replace-all">
  108.                 <xsl:with-param name="text" select="$vnotes_1"/>
  109.                 <xsl:with-param name="replace" select="'\r'"/>
  110.                 <xsl:with-param name="by" select="''"/>
  111.               </xsl:call-template>
  112.             </xsl:variable>
  113.             <xsl:variable name="vnotes_3">
  114.               <xsl:call-template name="string-replace-all">
  115.                 <xsl:with-param name="text" select="$vnotes_2"/>
  116.                 <xsl:with-param name="replace" select="' '"/>
  117.                 <xsl:with-param name="by" select="$delimiter"/>
  118.               </xsl:call-template>
  119.             </xsl:variable>
  120.             <xsl:variable name="vnotes_4">
  121.               <xsl:call-template name="string-replace-all">
  122.                 <xsl:with-param name="text" select="$vnotes_3"/>
  123.                 <xsl:with-param name="replace" select="' '"/>
  124.                 <xsl:with-param name="by" select="''"/>
  125.               </xsl:call-template>
  126.             </xsl:variable>
  127.             <xsl:value-of select="string($vnotes_4)"/>
  128.           </notes>
  129.           <uuid>
  130.             <xsl:value-of select="string(uuid)"/>
  131.           </uuid>
  132.           <ctimex>
  133.             <xsl:value-of select="string(creationtime)"/>
  134.           </ctimex>
  135.           <atimex>
  136.             <xsl:value-of select="string(lastaccesstime)"/>
  137.           </atimex>
  138.           <xsl:if test="string(((normalize-space($vexpires) = 'true') or (normalize-space($vexpires) = '1'))) != 'false'">
  139.             <xtimex>
  140.               <xsl:value-of select="string($vexpiretime)"/>
  141.             </xtimex>
  142.           </xsl:if>
  143.           <pmtimex>
  144.             <xsl:value-of select="$vlastmodtime"/>
  145.           </pmtimex>
  146.           <rmtimex>
  147.             <xsl:value-of select="$vlastmodtime"/>
  148.           </rmtimex>
  149.         </entry>
  150.       </xsl:for-each>
  151.       <xsl:value-of select="$crlf" disable-output-escaping="yes"/>
  152.       <xsl:value-of select="$crlf" disable-output-escaping="yes"/>
  153.     </passwordsafe>
  154.   </xsl:template>
  155.  
  156.   <xsl:template name="string-replace-all">
  157.     <xsl:param name="text"/>
  158.     <xsl:param name="replace"/>
  159.     <xsl:param name="by"/>
  160.     <xsl:choose>
  161.       <xsl:when test="contains($text, $replace)">
  162.         <xsl:value-of select="substring-before($text, $replace)"/>
  163.         <xsl:value-of select="$by"/>
  164.         <xsl:call-template name="string-replace-all">
  165.           <xsl:with-param name="text" select="substring-after($text, $replace)"/>
  166.           <xsl:with-param name="replace" select="$replace"/>
  167.           <xsl:with-param name="by" select="$by"/>
  168.         </xsl:call-template>
  169.       </xsl:when>
  170.       <xsl:otherwise>
  171.         <xsl:value-of select="$text"/>
  172.       </xsl:otherwise>
  173.     </xsl:choose>
  174.   </xsl:template>
  175. </xsl:stylesheet>
  176.